package com.xindacloud.sms.console.system.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.xindacloud.sms.console.constant.SessionKeys;
import com.xindacloud.sms.console.result.Result;
import com.xindacloud.sms.console.result.ResultGenerator;
import com.xindacloud.sms.console.system.dto.SearchSmsPricingDTO;
import com.xindacloud.sms.console.system.dto.SmsPricingDTO;
import com.xindacloud.sms.console.system.dto.SmsUpdatePricingDTO;
import com.xindacloud.sms.console.system.dto.SmsUserAllowOperatorDTO;
import com.xindacloud.sms.console.system.dto.UpdateOperatorsDTO;
import com.xindacloud.sms.console.system.entity.SmsPricing;
import com.xindacloud.sms.console.system.entity.SmsUser;
import com.xindacloud.sms.console.system.service.ISmsPricingService;
import com.xindacloud.sms.console.system.service.ISmsUserAllowOperatorService;
import com.xindacloud.sms.console.system.vo.SearchSmsPricingPageVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 价格列表 前端控制器
 * </p>
 *
 * @author czc
 * @since 2019-10-08
 */
@Api(tags = {"价格接口管理模块"})
@RestController
@RequestMapping("/api/v1/sms-pricing")
public class SmsPricingController {

    @Autowired
    private ISmsPricingService smsPricingService;

    @Autowired
    ISmsUserAllowOperatorService allowOperatorService;

    @ApiOperation(value = "价格列表", response = SmsPricing.class)
    @PostMapping(value = "/getPricingList")
    public Result getPricingList(@ApiParam(value = "查询运营商价格列表DTO", name = "smsPricingDTO")
                                 @RequestBody SmsPricingDTO smsPricingDTO) {
        return smsPricingService.getPricingList(smsPricingDTO);
    }

    @ApiOperation(value = "分页查询用户价格列表", response = SearchSmsPricingPageVO.class)
    @PostMapping(value = "/page")
    public Result queryPagePricing(@ApiParam(value = "分页查询用户价格列表DTO", name = "searchSmsPricingDTO")
                                   @RequestBody SearchSmsPricingDTO searchSmsPricingDTO,HttpServletRequest request) {
    	HttpSession session = request.getSession();
	    SmsUser user = (SmsUser) session.getAttribute(SessionKeys.ACCOUNT);
	    searchSmsPricingDTO.setOperatorId(user.getUserId());
        return ResultGenerator.genSuccessResult(smsPricingService.queryPricingPageBy(searchSmsPricingDTO));
    }


    @ApiOperation(value = "修改运营商状态", notes = "返回200为修改成功,其他返回码均为异常")
    @PostMapping(value = "/updateStatus")
    public Result updateStatus(@ApiParam(value = "修改运营商状态", name = "smsPricingDTO")
                               @RequestBody SmsPricingDTO smsPricingDTO) {
        return smsPricingService.updateStatus(smsPricingDTO);
    }

    @ApiOperation(value = "根据用户授权是否禁用运营商", notes = "返回200为修改成功,其他返回码均为异常")
    @PostMapping(value = "/allow-operator")
    public Result updateUserAllowOperator(@ApiParam(value = "根据用户授权是否禁用运营商", name = "updateOperatorsDTO")
                                          @RequestBody UpdateOperatorsDTO updateOperatorsDTO) {
        final boolean updateResult = allowOperatorService.updateUserAllowOperator(updateOperatorsDTO.getUserId(),
                updateOperatorsDTO.getOperatorIds(), updateOperatorsDTO.getStatus());
        return ResultGenerator.genSuccessResult("修改结果:" + updateResult);
    }

    @ApiOperation(value = "导入价格表(后缀为.xls的Excel文件)", notes = "返回200为修改成功,其他返回码均为异常")
    @PostMapping(value = "/importExcel")
    public Result importUserPriceExcel(@ApiParam(name = "file", value = "文件", required = true)
    @RequestParam("file") MultipartFile file,
    @ApiParam(name = "userId", value = "用户ID", required = true)
    @RequestParam(value = "userId", name = "userId") Integer userId) {
        return allowOperatorService.importUserPriceExcel(file, userId);
    }
    
    @ApiOperation(value = "下载所有价格")
    @PostMapping(value = "/downloadAllPrice")
    public void downloadAllPrice(HttpServletRequest request,HttpServletResponse response,
    @RequestBody SmsUserAllowOperatorDTO smsUserAllowOperatorDTO) {    	
    	allowOperatorService.downloadAllPrice(request,response,smsUserAllowOperatorDTO.getUserId());
    }
    
    @ApiOperation(value = "上传所有价格")
    @PostMapping(value = "/uploadAllPrice")
    public Result uploadAllPrice(@RequestParam("file") MultipartFile file,@RequestParam(value = "userId", name = "userId") Integer userId) {
         return allowOperatorService.uploadAllPrice(file,userId);
    }
    
    @ApiOperation(value = "批量修改价格")
    @PostMapping(value = "/updateBatchPrice")
    public Result updateBatchPrice(@RequestBody SmsUpdatePricingDTO smsUpdatePricingDTO) {
         return allowOperatorService.updateBatchPrice(smsUpdatePricingDTO);
    }
    
    @ApiOperation(value = "修改用户价格")
    @PostMapping(value = "/updateUserPrice")
    public Result updateUserPrice(@RequestBody SmsUserAllowOperatorDTO smsUserAllowOperatorDTO) {
         return allowOperatorService.updateUserPrice(smsUserAllowOperatorDTO);
    }

}
